﻿using System;
using System.Collections.Generic;
using UnityEditor;
using UnityEngine;

[Serializable]
internal class EditorEventObjectMap
{
    private static Dictionary<USEventBase, USEventBaseEditor> data;

    public static void Clean()
    {
        if (data != null)
        {
            foreach (KeyValuePair<USEventBase, USEventBaseEditor> pair in data)
            {
                UnityEngine.Object.DestroyImmediate(pair.Value);
            }
            data.Clear();
        }
    }

    public static USEventBaseEditor GetKey(USEventBase eventBase)
    {
        if (Data.ContainsKey(eventBase) && (Data[eventBase] == null))
        {
            Data.Remove(eventBase);
        }
        if (!Data.ContainsKey(eventBase))
        {
            System.Type aBaseClass = typeof(USEventBaseEditor);
            System.Type type = aBaseClass;
            foreach (System.Type type3 in USEditorUtility.GetAllSubTypes(aBaseClass))
            {
                if (type3.IsSubclassOf(aBaseClass))
                {
                    foreach (Attribute attribute in type3.GetCustomAttributes(true))
                    {
                        CustomEditor editor = attribute as CustomEditor;
                        if (editor.m_InspectedType == eventBase.GetType())
                        {
                            type = type3;
                        }
                    }
                }
            }
            USEventBaseEditor editor2 = ScriptableObject.CreateInstance(type) as USEventBaseEditor;
            Data.Add(eventBase, editor2);
            editor2.TargetEvent = eventBase;
        }
        return Data[eventBase];
    }

    private static Dictionary<USEventBase, USEventBaseEditor> Data
    {
        get
        {
            if (data == null)
            {
                data = new Dictionary<USEventBase, USEventBaseEditor>();
            }
            return data;
        }
    }
}

